<?php

// 分组
class AdminGroupsAction extends Action {
	
	private $cLogs = null;
	
	private $cAdminGroups = null;
	
	function __construct(){

		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
		import('@.ORG.AdminGroups');
		import('@.ORG.Logs');
		
		$this->cAdminGroups = new AdminGroups();
		$this->cLogs = new Logs();
		
	}
	
	//列表
    public function index(){

    	admin_role('AdminGroups:index');

    	$adminGroups = $this->cAdminGroups->lists();
       
    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'admin_groups'	=> $adminGroups,
    			'MainPage'		=> 'AdminGroups:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){

    	admin_role('AdminGroups:add');

        //权限列表
        import('@.ORG.Roles');
        $cRoles = new Roles();

        $roles  = $cRoles->lists();

    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
                'roles'     => $roles,
    			'MainPage'	=> 'AdminGroups:add'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add_do(){

    	admin_role('AdminGroups:add');
    	
   		$post = post(array(
    		array('group_name'		,'trim',''),
    		array('group_code'		,'trim',''),
    		array('group_roles'		,'trim','')
    	));

    	$add_result = $this->cAdminGroups->add($post);
    
    	if($add_result > 0){
    		
    		$this->cLogs->add('AdminGroups:add','添加分组成功:'.$post['group_name']);
    		
    		json_success('分组保存成功');
    	}else{
    		json_error('分组保存失败');
    	}
    }
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('AdminGroups:edit');
    	
    	$get = get(array(
    		array('id','intval',0)
    		
    	));

    	$id = $get['id'];
    	
    	$group = $this->cAdminGroups->get_by_group_id($id);

        //权限列表
        import('@.ORG.Roles');
        $cRoles = new Roles();

        $roles  = $cRoles->lists();

        //判断哪个有权限
        $group_roles = explode(',',$group['group_roles']);

        foreach ($roles as $i => $r){
            $roles[$i]['has_role'] = in_array($r['role_value'], $group_roles);
        }


    	$this->assign(
    		array(
    			'APP_NAME'		=> APP_NAME,
    			'group'			=> $group,
                'roles'         => $roles,
    			'MainPage'		=> 'AdminGroups:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function update(){
    	
    	admin_role('AdminGroups:edit');
    	 
    	$post = post(array(
    		array('group_id'		,'intval',0),
    		array('group_name'		,'trim',''),
    		array('group_code'		,'trim',''),
    		array('group_roles'		,'trim','')
    	));
   	
    	$id = $post['group_id'];
    	
    	unset($post['group_id']);
    
    	$update_result = $this->cAdminGroups->update($id,$post);
    	
    	if($update_result){
    		
    		$this->cLogs->add('AdminGroups:edit','分组更新成功:'.$post['group_name']);
    		
    		json_success('保存成功');
    	}else{
    		json_error('保存失败');
    	}
    }
}